<?php

namespace App\Http\Controllers\Admin;

use App\Http\Common\Lib\Common;
use App\Models\Menu;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MenuController extends BaseAdminController
{
    //
    private $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = new Menu();
    }

    /**
     * 列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View|\think\response\View
     */
    public function index(){
        if(\request()->ajax()){
            $list = $this->model->getList(0,[['status','>',-1]]);
            return response()->json(['code'=>0,'msg'=>'ok','data'=>$list]);
        }else{
            return view('admin.menu.index');
        }
    }
    /**
     * 添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function add(){
        return view('admin.menu.add');
    }
    /**
     * 保存添加数据
     * @return \Illuminate\Http\JsonResponse
     */
    public function addStore(){
       $input = \request()->all();
       $input['url'] = \request()->input('url','');
        $res = $this->model->add($input);
        return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }
    /**
     * 编辑页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function edit(){
        return view('admin.menu.edit');
    }
    /**
     * 保存添加数据
     * @return \Illuminate\Http\JsonResponse
     */
    public function editStore(){
        $input = \request()->all();
        $input['url'] = \request()->input('url','');
        $res = $this->model->edit($input);
        return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }
    /**
     * 修改状态
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeStatus(){
        $id = \request()->input('id',0);
        $opt = \request()->input('opt','');
        $res = $this->model->changeStatus($id,$opt);
        return response()->json(Common::dataFormat($res['status'],$res['msg']));
    }

    /**
     * 修改排序
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeSort(){
        $id = \request()->input('id',0);
        $value = \request()->input('value',9);
        $res = $this->model->changeColumn($id,'sort_no',$value,null,'sortNo');
        return response()->json(Common::dataFormat($res['status'],$res['msg']));
    }
    /**
     * 修改是否菜单
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeMenu(){
        $id = \request()->input('id',0);
        $value = \request()->input('value',9);
        $res = $this->model->changeColumn($id,'is_menu',$value,null,'menu');
        return response()->json(Common::dataFormat($res['status'],$res['msg']));
    }
    /**
     * 修改是否权限检查
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeCheck(){
        $id = \request()->input('id',0);
        $value = \request()->input('value',9);
        $res = $this->model->changeColumn($id,'is_checked',$value,null,'check');
        return response()->json(Common::dataFormat($res['status'],$res['msg']));
    }

    /**
     * 详情
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|\think\Response
     */
    public function detail(){
        $id = \request()->input('id',0);
        $res = $this->model->findOne($id);
        return response(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }
    /**
     * 下拉选项
     * @return \Illuminate\Http\JsonResponse
     */
    public function selectList(){
        $list = $this->model->selectList();
        return response()->json(Common::dataFormat(config('statusInfo.status.success'),config('statusInfo.msg.success'),$list));
    }
}
